package cn.com.wangkai.wangkaisiji;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.com.wangkai.wangkaisiji.adapter.LvAdapter;
import cn.com.wangkai.wangkaisiji.bean.CartBean;
import cn.com.wangkai.wangkaisiji.bean.OrderBean;
import cn.com.wangkai.wangkaisiji.utils.Conts;
import cn.com.wangkai.wangkaisiji.utils.JSONParser;

public class OrderActivity extends BasetActivity {
    @ViewInject(R.id.ed_order)
    private EditText edOrder;
    @ViewInject(R.id.iv_resert)
    private ImageView ivRestert;
    private String token="";
    private int page=0;
    private OrderBean b=new OrderBean();
    private List<OrderBean> gro=new ArrayList<>();
    private Map<String , List<CartBean>> chi=new HashMap<>();
    private LvAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);
        x.view().inject(this);
        init();
        load();
        listener();
    }

    private void init() {

        token=getIntent().getStringExtra("token");

    }

    private void load() {
        RequestParams params=new RequestParams(Conts.ORDER_URL);
        params.addBodyParameter("page",page+"");
        params.addBodyParameter("token",token);
        params.addBodyParameter("orderId",edOrder.getText().toString());
        x.http().get(params, new Callback.CommonCallback<JSONObject>() {
            @Override
            public void onSuccess(JSONObject result) {
                Log.e("result","result"+result.toString());
                try {
                    if (result.getInt("code")==1){
                        JSONArray jdata=result.getJSONArray("data");
                        for (int i=0;i<jdata.length();i++){
                            if(jdata.getJSONObject(i).getString("status").equals("1")){
                                JSONParser.typeOneOrThree(jdata,i);
                            }else if(jdata.getJSONObject(i).getString("status").equals("-2")){
                                JSONParser.typeOneOrThree(jdata,i);
                            }else{
                                if (jdata.getJSONObject(i).getString("status").equals("-2")){
                                    //Log.e("", "type为2 不需要拆分"+jdata.getJSONObject(i).getString("type"));
                                    JSONParser.typeOneOrThree(jdata,i);
                                }else{
                                    Log.e("", "状态不为1type 需要拆分");

                                    //需要拆分的
                                    //Log.e("", "需要拆分的");
                                    JSONArray jchild=jdata.getJSONObject(i).getJSONArray("childOrders");
                                    for (int j=0;j< jchild.length();j++){
                                        Log.e("", "第" + j + "个需要拆分的======" + jchild.getJSONObject(j).getString("orderId"));
                                        JSONParser.typeOneOrThree(jdata,i);
                                        if (jchild.getJSONObject(j).getJSONArray("items").length()<=1){
                                                b.setIsSingle(true);
                                        }

                                        if (b.getPayment().equals("5")&&b.getStatus().equals("3")) {

                                            gro.add(b);
                                        }

                                            List<CartBean> cartbeens=JSONParser.parseOrderList(jchild);
                                            chi.put(jchild.getJSONObject(j).getString("orderId"),cartbeens);

                                    }
                                    //TODO
                                }
                            }
                        }
                        if (adapter==null){
                            adapter=new LvAdapter(chi,OrderActivity.this,gro);
                        }else{
                            adapter.notifyDataSetChanged();
                        }
                    }else{
                        Toast.makeText(OrderActivity.this, (Integer) result.get("message"),Toast.LENGTH_SHORT).show();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(OrderActivity.this,"网络异常",Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });


    }

    private void listener() {
            ivRestert.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    edOrder.setText("");

                }
            });
    }


}
